<?php include('header.php');?>
<div class="container-fluid">
	<div class="row-fluid">
		<div class="span 12">
			<h1>ADMIN</h1>
		</div>
	</div>
	<div class="row-fluid">
		<div class="span2"">
			<?php include('lefter.php');?>
		</div>
		<div class="span10">
			<div class="row-fluid">
				<div class="span8">
					<h3>
						<span>BLOG EDIT</span>
					</h3>
				</div>
			</div>
			<hr />
			<form id="view_form" class="form-horizontal"  action="<?=base_url('admin/blog_edit_action')?>" method="post" enctype="multipart/form-data">
			<input type="hidden" name="id" value="<?=$blog_view_row['id']?> "/>
			<input type="hidden" name="limit" value="<?=$limit?>" />
			<input type="hidden" name="offset" value="<?=$offset?>" />
			<div class="control-group">
				<label class="control-label" for="title"><strong>제목</strong></label>
				<div class="controls">
					<input type="text" name="title" id="title" value="<?=$blog_view_row['title']?>" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="date"><strong>날짜(화면표시)</strong></label>
				<div class="controls">
					<input type="text" name="date" id="date"  value="<?php if($blog_view_row['date'] != '0000-00-00') :?><?=date("Y.m", strtotime($blog_view_row['date']))?><?php endif;?>" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="client"><strong>클라이언트</strong></label>
				<div class="controls">
					<input type="text" name="client" id="client" value="<?=$blog_view_row['client']?>" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="category_id"><strong>카테고리</strong></label>
				<div class="controls">
				<?php foreach($category_rows as $row) :?>
					<label class="checkbox inline"> 
						<input type="checkbox" name="category_id_<?=$row['id']?>" value="<?=$row['id']?>" <?php if(in_array($row['id'], $blog_view_row['category_id'])) :?>checked<?php endif;?> /> <?=$row['title']?>
					</label>
				<?php endforeach;?>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label"><strong>썸네일</strong></label>
				<div class="controls">
					<div id="select_image_div">
					<?php foreach($blog_view_row['image'] as $row) :?>
					<div class="fileupload fileupload-exists" data-provides="fileupload">
						<input type="hidden" name="<?=$row['id']?>" value="<?=$row['id']?>" />
						<div class="fileupload-preview thumbnail save-image-div"><?php if(empty($row['is_image'])) :?><?=$row['orig_name']?><?php else :?><img src="<?=UPLOAD_DIR?>/<?=$row['file_name']?>" /><?php endif;?></div>   
						<div>
							<a href="#" class="btn fileupload-exists btn-image-remove" data-dismiss="fileupload">Remove</a>
						</div>  
					</div>
					<?php endforeach;?>
					</div>
					<input type="hidden" name="image_count" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="content"><strong>본문</strong></label>
				<div class="controls">
					<textarea id="content" name="content"><?=$blog_view_row['content']?></textarea>
				</div>
			</div>
			<div class="row-fluid">
				<div class="span4 offset8">
					<div class="pull-right">
						<button type="button" class="btn btn-cancel">Cancel</button>
						<button type="submit" class="btn btn-primary">Save</button>
					</div>
				</div>
			</div>
			</form>
		</div>
	</div>
</div>
<script src="<?=JS_DIR?>/jquery-1.8.3.min.js"></script>
<script src="<?=JS_DIR?>/respond.min.js"></script>
<script src="<?=BS_DIR?>/admin/js/bootstrap.min.js"></script>
<script src="<?=JS_DIR?>/jquery.tmpl.min.js"></script>
<script src="<?=BS_DIR?>/admin/js/bootstrap-fileupload.min.js"></script>
<script src="<?=JU_DIR?>/js/jquery-ui-1.9.2.custom.min.js"></script>
<script src="<?=CK_DIR?>/ckeditor.js"></script>
<script>
var select_image_object = {
	number: 1,
	add: function() {
		$("input[name='image_count']").val(select_image_object.number);
		var data = {name: select_image_object.number};
		$("#select_image").tmpl(data).appendTo("#select_image_div");
		select_image_object.number++;
	}
};

$(function() {
	<?php if(empty($blog_view_row['image'])) :?>select_image_object.add();<?php endif;?>
	$("#date").datepicker({dateFormat: "yy.mm"});
	$("#view_form").on("submit", function() {
		if(!$("#title").val()) {
			alert("제목을 입력해 주세요");
			$("#title").focus();
			return false;
		}
	});
	$(document).on("click", "a.btn-image-remove", function() {
		select_image_object.add();
		$(this).parent().parent().remove();
	});
	$("button.btn-cancel").click(function() {
		location.href = "<?=base_url('admin/blog/'.$limit.'/'.$offset)?>";
	});
	CKEDITOR.replace( 'content', {
	    filebrowserUploadUrl: "<?=base_url('admin/ckeditor_image_upload')?>"
	});
});
</script>
<script id="select_image" type="text/x-jquery-tmpl">
    <div class="fileupload fileupload-new" data-provides="fileupload">
		<div class="fileupload-preview thumbnail"></div>
		<div>
			<span class="btn btn-file"><span class="fileupload-new">Select image</span><span class="fileupload-exists">Change</span><input type="file" name="image_${name}"/></span>
			<a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
		</div>
	</div>
</script>
<?php include('footer.php');?>